package com.dao.airticle;

import com.mapper.ArticleMapper;
import com.pojo.Article;
import com.pojo.Comment;
import com.pojo.MangeArticle;
import com.pojo.Operation;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class ArticleDao {
     /**
      * 根据条件筛选自己的博客
      */
     public static List<Article> get_all_blog(MangeArticle mangeArticle)throws IOException{
          System.out.println("[info]获取博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_all_blog(mangeArticle);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
          return articles;
     }

     /**
      * 获取收藏的博客
      */
     public static List<Article> get_collection_list(String userid)throws IOException{
          System.out.println("[info]获取收藏列表");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_collection_list(userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
          return articles;
     }

     /**
      * 获取草稿列表
      */
     public static List<Article> get_draft_list(String article_author)throws IOException{
          System.out.println("[info]获取草稿列表");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_draft_list(article_author);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
          return articles;
     }

     /**
      * 删除博客
      */
     public static void del_blog(String blog_id)throws IOException{
          System.out.println("[info]删除博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.del_blog(blog_id);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
     }

     /**
      * 编辑博客
      */
     public static void editor_blog(Article article)throws IOException{
          System.out.println("[info]编辑博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.editor_blog(article);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
     }

     /**
      * 获取近期文章
      */
     public static List<Article> get_recent_blog(String article_author)throws IOException{
          System.out.println("[info]获取近期文章");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_recent_blog(article_author);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      * 获取专栏详情中的博客
      */
     public static List<Article> get_column_info_blog(int page_number,String column_name)throws IOException{
          System.out.println("[info]获取专栏中的博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_column_info_article(page_number,column_name);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      * 查看点赞数据是否存在
      */
     public static boolean check_support(String blog_id,String userid)throws IOException{
          System.out.println("[info]查看点赞数据是否存在");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          String id=articleMapper.check_support(blog_id,userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return id == null;
     }

     /**
      * 创建点赞数据
      */
     public static void add_support(String blog_id,String userid)throws IOException{
          System.out.println("[info]点赞博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.add_support(blog_id,userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

     }

     /**
      * 改变点赞状态
      */
     public static void change_support_status(String blog_id,String userid,String is_delete)throws IOException{
          System.out.println("[info]改变点赞状态");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.change_support_status(blog_id, userid,is_delete);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
     }

     /**
      * 查看收藏数据是否存在
      */
     public static boolean check_collection(String blog_id,String userid)throws IOException{
          System.out.println("[info]查看收藏数据是否存在");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          String id=articleMapper.check_collection(blog_id,userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return id == null;
     }

     /**
      * 添加收藏数据
      */
     public static void add_collection(String blog_id,String userid)throws IOException{
          System.out.println("[info]收藏博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.add_collection(blog_id, userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

     }

     /**
      * 改变收藏状态
      */
     public static void change_collection_status(String blog_id,String userid,String is_delete)throws IOException{
          System.out.println("[info]改变收藏状态");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.change_collection_status(blog_id, userid, is_delete);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
     }

     /**
      * 获取用户对文章的操作
      */
     public static Operation get_user_operation(String blog_id, String userid)throws IOException{
          System.out.println("[info]获取用户对文章已做的操作");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
           Operation operation1=articleMapper.get_operation_support(blog_id, userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return operation1;
     }

     /**
      *获取用户对文章的操作2
      */
     public static Operation get_user_is_collection(String blog_id, String userid)throws IOException{
          System.out.println("[info]获取用户对文章已做的操作");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          Operation operation=articleMapper.get_operation_collection(blog_id,userid);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return operation;
     }

     /**
      * 发布评论
      */
     public static String write_comment(Comment comment)throws IOException{
          System.out.println("[info]发布评论");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.write_comment(comment);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return comment.getId();
     }

     /**
      * 获取文章一级评论
      */
     public static List<Comment> get_article_comment(String blog_id)throws IOException{
          System.out.println("[info]获取一级评论");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Comment> comments=articleMapper.get_article_comment(blog_id);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return comments;
     }

     /**
      * 获取二级评论
      */
     public static List<Comment> get_next_comment(List<Comment> comments)throws IOException{
          System.out.println("[info]获取二级评论");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          for(Comment comment:comments)
          {
               List<Comment>comments1=articleMapper.get_next_comment(comment);
               comment.setChild_list(comments1);
          }
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return comments;
     }

     /**
      * 获取热门推荐
      */
     public static List<Article> get_hot_blog()throws IOException{
          System.out.println("[info]获取热门推荐");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_hot_blog();
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      * 获取最新博文
      */
     public static List<Article> get_latest_blog()throws IOException{
          System.out.println("[info]获取最新博文");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_latest_blog();
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      * 查询未审核文章的总记录数
      */
     public static int get_not_check_blog_count()throws IOException{
          System.out.println("[info]获取未审核未审核文章的总记录数");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
           int totalCount=articleMapper.get_not_check_totalCount();
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return totalCount;
     }

     /**
      * 查询所有文章的数量
      */
     public static int get_manage_blog_count()throws IOException{
          System.out.println("[info]获取文章总数");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          int totalCount=articleMapper.get_all_blog_count();
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return totalCount;
     }

     /**
      * 查询当前页的未审核文章
      */
     public static List<Article> get_current_not_check(int start)throws IOException{
          System.out.println("[info]获取未审核的文章");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
         List<Article> articles=articleMapper.get_not_check_blog(start);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      * 查询所有文章
      */
     public static List<Article> get_all_article(int start)throws IOException{
          System.out.println("[info]获取所有文章");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          List<Article> articles=articleMapper.get_all_article(start);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();

          return articles;
     }

     /**
      *  通过博客
      */
     public static void pass_blog(String blog_id)throws IOException{
          System.out.println("[info]通过博客");
          //获取sqlSession对象
          SqlSession sqlSession= MybatisUtil.getSqlSession();
          //3.获取Mapper接口的代理对象
          ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
          //4.执行方法
          articleMapper.pass_blog(blog_id);
          //提交事务
          sqlSession.commit();
          //5.释放资源
          sqlSession.close();
     }

     /**
      * 博客审核不通过
      */
      public static void not_pass_blog(String blog_id)throws IOException{
           System.out.println("[info]博客审核不通过");
           //获取sqlSession对象
           SqlSession sqlSession= MybatisUtil.getSqlSession();
           //3.获取Mapper接口的代理对象
           ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
           //4.执行方法
           articleMapper.not_pass_blog(blog_id);
           //提交事务
           sqlSession.commit();
           //5.释放资源
           sqlSession.close();
      }
}
